<template>
  <div class="agency">
    <div class="user-main">
      <div class="bgc"></div>
      <div class="user-info">
        <div class="lefts">
          <div class="logo" v-if="isShow">
            <img :src="userDate.imgs" alt="" />
          </div>
          <div class="logo" v-else>
            <!-- <img src="@/assets/矩形 2.png" alt="" /> -->
          </div>
          <div class="user-mains">
            <div class="user-name" v-if="isShow">{{ userDate.name }}</div>
            <div class="user-name" v-else>请登录</div>
            <div class="card" v-if="isShow">
              <div class="card-icon">
                <img src="@/assets/user-icon.png" alt="" />
              </div>
              <span>导师</span>
            </div>
            <div class="card login" v-else>
              <span @click="loginClick">点击登录</span>
            </div>
          </div>
        </div>
        <div class="rights">编辑资料</div>
      </div>
      <div class="user-info-bottom">
        <div class="loan">
          <div class="loan-top" @click="loansClick">
            <span>贷款金额</span>
            <span class="loan-icons">
              <img src="@/assets/enter-icons.png" alt="" />
            </span>
          </div>
          <div class="loan-bottom">
            <div class="money">¥</div>
            <span class="price">20000</span>
            <img src="@/assets/user-eye.png" alt="" />
          </div>
        </div>
        <div class="loan">
          <div class="loan-top" @click="widthClick">
            <span>提现金额</span>
            <span class="loan-icons">
              <img src="@/assets/enter-icons.png" alt="" />
            </span>
          </div>
          <div class="loan-bottom">
            <div class="money">¥</div>
            <span class="price">20000</span>
            <img src="@/assets/user-eye.png" alt="" />
          </div>
        </div>
      </div>
    </div>
    <div class="agency-mai">
      <div class="take">
        <div class="take-top" @click="goOrder">
          <div class="top-lefts">提货订单</div>
          <div class="top-rights">
            全部订单
            <span class="top-icons">
              <img src="@/assets/enter-icons.png" alt="" />
            </span>
          </div>
        </div>
        <div class="item-main">
          <div
            class="items"
            v-for="(item, index) in itemList"
            :key="index"
            @click="itemClick(index)"
          >
            <div class="imgs">
              <img :src="item.icon" alt="" />
            </div>
            <p>{{ item.title }}</p>
          </div>
        </div>
      </div>
      <div class="commenly">
        <div class="take-top">
          <div class="top-lefts">常用功能</div>
        </div>
        <div class="commenly-main">
          <div
            class="items"
            v-for="(item, index) in commenlyList"
            :key="index"
            @click="commenlyClick(item)"
          >
            <div class="imgs">
              <img :src="item.icon" alt="" />
            </div>
            <p>{{ item.title }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { Dialog } from 'vant'
import { getLogin, getUser, getWxJssdk } from '@/api/profile'
const appid = `wx1c5ad3bcd686d718`
// 内网穿透 可以是微信服务器访问到本地
// const redirect_uri = 'https://daili.meiqiewangluo.com'
const redirect_uri = window.location.href
//redirect_uri = http://127.0.0.1/agency
//redirect_uri = window.location.href;
console.log(window.location.href)
export default {
  name: 'agency',
  components: {},
  data() {
    return {
      itemList: [
        {
          title: '待付款',
          icon: require('@/assets/delay-pay.png'),
        },
        {
          title: '待发货',
          icon: require('@/assets/toSend.png'),
        },
        {
          title: '待收货',
          icon: require('@/assets/forSend.png'),
        },
        {
          title: '退换货',
          icon: require('@/assets/to-money.png'),
        },
        {
          title: '已完成',
          icon: require('@/assets/done.png'),
        },
      ],
      commenlyList: [
        {
          title: '补货',
          icon: require('@/assets/replenishment.png'),
        },
        {
          title: '云仓',
          icon: require('@/assets/yunCang.png'),
        },
        {
          title: '招商',
          icon: require('@/assets/merchants.png'),
        },
        {
          title: '权益',
          icon: require('@/assets/equity.png'),
        },
        {
          title: '发圈',
          icon: require('@/assets/circle.png'),
        },
        {
          title: '风云榜',
          icon: require('@/assets/prominent.png'),
        },
        {
          title: '粉丝',
          icon: require('@/assets/fans.png'),
        },
        {
          title: '升级',
          icon: require('@/assets/leaveUp.png'),
        },
        {
          title: '地址',
          icon: require('@/assets/address.png'),
        },
        {
          title: '通知',
          icon: require('@/assets/notice.png'),
        },
      ],
      isShow: false,
      // code: '011qwTFa1vl1aA0Z49Ia13652Q2qwTFm',
      // code: '011VkZkl2WQ2b64FBGll29k7AP1VkZkk',
      code: '0914Gf0w3GavvV2aqG2w3PZspx14Gf09',
      userDate: {},
      path: '',
    }
  },
  created() {
    this.path = this.$router.history.current.path
    this.getItems()
    this.getUserDates()
    this.getUsers()
    this.getWxJssdks()
  },
  methods: {
    getUserDates() {
      let userInfo = localStorage.getItem('userInfo')
      if (userInfo) {
        this.userDate = JSON.parse(localStorage.getItem('userInfo'))
      }
    },
    getItems() {
      this.code = this.$route.query.code
      let token = window.localStorage.getItem('token')
      if (token) {
        this.isShow = true
        this.getUserDates()
      }
      if (this.code || token) {
        const datas = {
          code: this.code,
        }
        getLogin(datas).then((res) => {
          //console.log(res)
          // let userId = res.data.appId
          const userInfo = {
            name: res.nickname,
            imgs: res.headimgurl,
          }
          if (res.token) {
            window.localStorage.setItem('token', res.token)
            window.localStorage.setItem('userInfo', JSON.stringify(userInfo))
            this.isShow = true
            this.$router.push({ path: this.path, query: {} })
            this.getUserDates()
          }
        })
      } else {
        Dialog.confirm({
          title: '用户还未登录,请先登录',
          // message: '弹窗内容',
        })
          .then(() => {
            this.loginClick()
          })
          .catch(() => {})
      }
    },

    // 登录
    loginClick() {
      // 1、引导用户进入授权页面同意授权，获取code
      var userinfo_api = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
      console.log(userinfo_api)
      location.href = userinfo_api
      this.code = this.$route.query.code
    },
    loansClick() {
      this.$router.push('/loans')
    },
    widthClick() {
      this.$router.push('/widthdrawral')
    },
    commenlyClick(item) {
      console.log(item)
      if (item.title == '招商') {
        // this.$router.push('/merchants')
        this.$router.push({
          name: 'merchants',
          query: {
            id: 1,
          },
        })
      } else if (item.title == '云仓') {
        this.$router.push({
          name: 'myCloud',
        })
      } else if (item.title == '补货') {
        this.$router.push({
          name: 'replenish',
        })
      } else if (item.title == '权益') {
        this.$router.push({
          name: 'interests',
        })
      } else {
        Toast('功能未开发')
      }
    },
    getUserInfo() {
      this.isShow = true
      this.getUserDates()
    },
    getLogins() {
      const datas = {
        code: this.code,
      }
      getLogin(datas).then((res) => {
        console.log(res)
        // let userId = res.data.appId
        const userInfo = {
          name: res.nickname,
          imgs: res.headimgurl,
        }
        if (res.token) {
          window.localStorage.setItem('token', res.token)
          window.localStorage.setItem('userInfo', JSON.stringify(userInfo))
          this.isShow = true

          this.getUserDates()
          this.$router.push({ path, query: {} })
        }
      })
    },
    goOrder() {
      this.$router.push('/orderForm')
    },
    itemClick(index) {
      if (index == 1 || index == 2 || index == 4) {
        if (index == 4) {
          index = 5
        }
        this.$router.push({
          path: '/orderForm',
          query: {
            status: index,
          },
        })
      } else {
        Toast('功能尚未开发')
      }
    },
    // 代理商信息
    getUsers() {
      getUser().then((res) => {
        console.log(res)
      })
    },
    // 获取jssdk
    getWxJssdks() {
      const datas = {
        url: window.location.href,
      }
      getWxJssdk(datas).then((res) => {
        console.log(res)
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.agency {
  font-size: 14px;
  // height: 100vh;
  height: 100%;
  overflow: auto;
  background: #f7f6fb;
}
.take {
  .take-top {
    display: flex;
    font-size: 16px;
    height: 38px;
    align-items: center;
    justify-content: space-between;
    color: #1f0d44;
    padding: 12px;
    font-weight: 600;
    .top-rights {
      font-size: 12px;
      font-weight: 400;
      color: #afabb7;
      display: flex;
      align-items: center;
      .top-icons {
        width: 20px;
        height: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        img {
          width: 12px;
          height: 12px;
        }
      }
    }
  }
}
.item-main {
  display: flex;
  height: 70px;
  .items {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .imgs {
      width: 24px;
      height: 24px;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.commenly {
  width: 351px;
  height: 264px;
  margin-top: 12px;
  background: #ffffff;
  border-radius: 8px;
  margin-bottom: 74px;
  .take-top {
    padding: 0 12px;
    font-size: 16px;
    height: 42px;
    line-height: 42px;
    color: #1f0d44;

    font-weight: 600;
  }
  .commenly-main {
    display: flex;
    flex-wrap: wrap;
    .items {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 86px;
      height: 70px;
      font-size: 12px;
      color: #665e75;
      .imgs {
        width: 24px;
        height: 25px;
        margin-bottom: 6px;
        img {
          width: 100%;
          height: 100%;
        }
      }
      &:nth-child(9) .imgs {
        width: 16px;
      }
    }
  }
}
.user-main {
  position: relative;
  width: 100%;
  height: 220px;
  background: #2c0659;
  .bgc {
    position: absolute;
    left: 0;
    top: 0;
    width: 130px;
    height: 115px;
    background: linear-gradient(
      130deg,
      rgba(255, 255, 255, 0) 0%,
      rgba(255, 255, 255, 0.06) 100%
    );
    border-radius: 0 40% 40% 40%;
  }
  .user-info {
    position: relative;
    z-index: 2;
    display: flex;
    padding: 24px 20px;
    width: 100%;
    justify-content: space-between;
    color: #fff;
    .lefts {
      display: flex;
      margin-right: 12px;
      .logo {
        width: 56px;
        height: 56px;
        border-radius: 50%;
        overflow: hidden;
        margin-right: 12px;
        background-color: pink;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .user-mains {
        flex: 1;
        font-size: 18px;
        font-weight: 600;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .card {
          font-size: 8px;
          width: 50px;
          height: 18px;
          background: linear-gradient(315deg, #622ccc 0%, #ff5bf4 100%);
          border-radius: 4px;
          display: flex;
          justify-content: center;
          align-items: center;
          .card-icon {
            width: 14px;
            height: 14px;
            img {
              width: 100%;
            }
          }
        }
        div.login {
          width: 64px;
          span {
            width: 100%;
            text-align: center;
          }
        }
      }
    }
    .rights {
      width: 84px;
      height: 26px;
      text-align: center;
      line-height: 26px;
      font-size: 12px;
      background: #622ccc;
      border-radius: 14px;
    }
  }
  .user-info-bottom {
    display: flex;
    color: #fff;
    margin: 0 20px;
    .loan {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      width: 165px;
      height: 36px;
      margin: 12px 0;
      padding-left: 20px;
      font-size: 12px;
      &:nth-child(1) {
        margin-right: 5px;
        border-right: 1px solid rgba(102, 94, 117, 0.5);
      }
      .loan-top {
        display: flex;
        align-items: center;
      }
      .loan-bottom {
        display: flex;
        .money {
          margin-top: 5px;
          width: 8px;
          height: 17px;
          font-size: 12px;
        }
        .price {
          font-weight: 600;
          color: #f7f6fb;
          font-size: 18px;
        }
        img {
          width: 20px;
          height: 18px;
          margin-top: 3px;
          margin-left: 9px;
        }
      }
      .loan-icons {
        width: 14px;
        height: 12px;
        margin-left: 3px;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
.agency-mai {
  padding: 0 12px;
  .take {
    position: relative;
    z-index: 2;
    height: 112px;
    background: #ffffff;
    border-radius: 8px;
    margin-top: -40px;
  }
}
</style>
